package com.xudow.app.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.activeshare.edu.ucenter.models.courseimages.CourseImagesVo;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xudow.app.Config;
import com.xudow.app.R;
import com.xudow.app.ui.task.CourseImagesTask;
import com.xudow.app.ui.widget.SquareCenterImageView;
import com.xudow.app.util.Lists;
import com.xudow.app.util.Maps;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * 课程图片Activity
 *
 * @author lukecheng
 */
public class CourseImagesActivity extends BaseActivity {

    private long courseId;

    private List<CourseImagesVo> images;
    private ImagesGridViewAdapter imagesGridViewAdapter;

    private GridView imageGridView;

    private CourseImagesTask courseImagesTask;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_school_images);

        courseId = getIntent().getLongExtra("courseId", 0L);

        images = Lists.newArrayList();
        imagesGridViewAdapter = new ImagesGridViewAdapter(images);

        imageGridView = (GridView) findViewById(R.id.image_grid);
        imageGridView.setAdapter(imagesGridViewAdapter);

        HashMap<String, String> params = Maps.newHashMap();
        params.put("course_id", "" + courseId);

        courseImagesTask = new CourseImagesTask(this, courseImagesTaskHandler);
        addTask(courseImagesTask);
        courseImagesTask.execute(params);
    }

    public void onBackBtnClick(View view) {
        finish();
    }

    private Handler courseImagesTaskHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (CourseImagesTask.SUCCESS == msg.what) {
                CourseImagesVo[] dataArr = (CourseImagesVo[]) msg.getData().getSerializable("images");
                images.addAll(Arrays.asList(dataArr));

                imagesGridViewAdapter.notifyDataSetChanged();
            }
        }
    };

    private class ImagesGridViewAdapter extends BaseAdapter {

        private List<CourseImagesVo> datas;

        public ImagesGridViewAdapter(List<CourseImagesVo> datas) {
            this.datas = datas;
        }

        @Override
        public int getCount() {
            return datas.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final SquareCenterImageView imageView = new SquareCenterImageView(CourseImagesActivity.this);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

            String url = String.format(Config.IMAGE_LOAD_URL_PARAMS, datas.get(position).getThumbnail());
            ImageLoader.getInstance().displayImage(url, imageView);

            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String imageUrl = String.format(Config.IMAGE_LOAD_URL_PARAMS, datas.get(position).getImgPath());

                    ImageViewPopup imageViewPopup = new ImageViewPopup(CourseImagesActivity.this, imageUrl);
                    imageViewPopup.showAtLocation(findViewById(R.id.root), Gravity.CENTER, 0, 0);
                }
            });

            return imageView;
        }

    }

}
